package com.ossjk.qlh.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.loginfo.LogModule;
import com.ossjk.qlh.loginfo.LogOperation;
import com.ossjk.qlh.system.entity.Department;
import com.ossjk.qlh.system.entity.Permission;
import com.ossjk.qlh.system.entity.Role;
import com.ossjk.qlh.system.service.IDepartmentService;
import com.ossjk.qlh.system.service.IPermissionService;
import com.ossjk.qlh.system.service.IRolePermissionService;
import com.ossjk.qlh.system.service.IRoleService;
import com.ossjk.qlh.system.vo.RoleVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-02-07
 */
@LogModule(value = "角色管理")
@Api(tags = "角色管理")
@RestController
@RequestMapping("/system/role")
public class RoleController extends BaseController {

	@Autowired
	private IRoleService iRoleService;
	@Autowired
	private IDepartmentService iDepartmentService;
	@Autowired
	private IPermissionService iPermissionService;
	@Autowired
	private IRolePermissionService iRolePermissionService;

	@LogOperation("列表")
	@ApiOperation(value = "列表")
	@PostMapping(value = "/list")
	@RequiresPermissions("00000015")
	public ResponseBean<List<Role>> list() {
		QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
		queryWrapper.orderByAsc("level", "sort");
		return responseSuccess(iRoleService.list(queryWrapper));
	}

	@LogOperation("去添加")
	@ApiOperation(value = "去添加")
	@PostMapping(value = "/toSave")
	@RequiresPermissions("000000150000")
	public ResponseBean<Map<String, Object>> toSave() {
		Map<String, Object> data = new HashMap<String, Object>();
		// 找到所有部门列表
		QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
		queryWrapper.orderByAsc("level", "sort");
		List<Department> departments = iDepartmentService.list(queryWrapper);
		if (!CommonUtil.isBlank(departments)) {
			data.put("departments", departments);
			return responseSuccess(data);
		} else {
			return responseFail();
		}
	}

	@LogOperation("去编辑")
	@ApiOperation(value = "去编辑")
	@PostMapping(value = "/toUpdate")
	@RequiresPermissions("000000150005")
	public ResponseBean<Map<String, Object>> toUpdate(@RequestBody RequestBean requestBean) {
		Map<String, Object> data = new HashMap<String, Object>();
		// 找到角色数据
		RoleVo roleVo = iRoleService.getVoById((String) requestBean.getParam().get("id"));
		if (!CommonUtil.isBlank(roleVo)) {
			data.put("roleVo", roleVo);
			// 找到所有部门列表
			QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
			queryWrapper.orderByAsc("level", "sort");
			List<Department> departments = iDepartmentService.list(queryWrapper);
			if (!CommonUtil.isBlank(departments)) {
				data.put("departments", departments);
			}
			return responseSuccess(data);
		} else {
			return responseFail();
		}
	}

	@LogOperation("添加")
	@ApiOperation(value = "添加")
	@PostMapping(value = "/save")
	@RequiresPermissions("000000150000")
	public ResponseBean save(@RequestBody RequestBean<RoleVo> requestBean) {
		RoleVo roleVo = requestBean.getData();
		if (iRoleService.saveVo(roleVo)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("编辑")
	@ApiOperation(value = "编辑")
	@PostMapping(value = "/update")
	@RequiresPermissions("000000150005")
	public ResponseBean update(@RequestBody RequestBean<RoleVo> requestBean) {
		RoleVo roleVo = requestBean.getData();
		if (iRoleService.updateVoById(roleVo)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("删除")
	@ApiOperation(value = "删除")
	@PostMapping(value = "/remove")
	@RequiresPermissions("000000150010")
	public ResponseBean delete(@RequestBody RequestBean requestBean) {
		String id = (String) requestBean.getParam().get("id");
		// 删除子节点
		if (iRoleService.removeChildrenById(id)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("去授权")
	@ApiOperation(value = "去授权")
	@PostMapping(value = "/toAuthority")
	@RequiresPermissions("000000150015")
	public ResponseBean<Map<String, Object>> toAuthority(@RequestBody RequestBean requestBean) {
		String id = (String) requestBean.getParam().get("id");
		Map<String, Object> data = new HashMap<String, Object>();
		// 查找该角色的所有权限
		List<String> pids = iRolePermissionService.queryPidByRid(id);
		data.put("pids", pids);
		// 查询所有权限
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.orderByAsc("level", "sort");
		data.put("permissions", iPermissionService.list(queryWrapper));
		return responseSuccess(data);
	}

	@LogOperation("授权")
	@ApiOperation(value = "授权")
	@PostMapping(value = "/authority")
	@RequiresPermissions("000000150015")
	public ResponseBean authority(@RequestBody RequestBean<Map<String, Object>> requestBean) {
		String rid = (String) requestBean.getData().get("id");
		List<String> pids = (List<String>) requestBean.getData().get("pids");
		if (iRolePermissionService.authorityByRid(rid, pids)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
